package com.sgb.kjwl.viewmodel.shopping;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.UrlConfig;
import com.sgb.kjwl.model.entity.ShopInfoEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.model.entity.user.CategoryChildEntity;
import com.sgb.kjwl.model.entity.user.CategoryParentsEntity;
import com.sgb.kjwl.repository.ShopCompanyInfoRepertory;
import com.sgb.kjwl.view.ui.activity.self.shop.CompanyShopInfoActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ShopConmpanyInfoModel extends BaseViewModel {
    public ObservableField<ShopInfoEntity> shopInfoEntity = new ObservableField<>(new ShopInfoEntity());
    private CompanyShopInfoActivity activity;
    private ShopCompanyInfoRepertory repertory;

    public ShopConmpanyInfoModel(CompanyShopInfoActivity activity, ShopCompanyInfoRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }


    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getAddress()
                .subscribe(new Consumer<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
                    @Override
                    public void accept(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> listBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.showAddressPop(listBaseEntity.getData());
                        MLog.d(AppConstant.API_LOG, "AddressEntity " + listBaseEntity.getData().get(0).getName());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //上传多图片
    public void uploadMutiFileModel(File file, int type, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "图片上传中"+completeNum+"/"+activity.imgTotal+"...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    activity.uploadRefresh(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void shopDetail(Map map) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().shopDetail(map).enqueue(new Callback<BaseEntity<ShopInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ShopInfoEntity>> call, Response<BaseEntity<ShopInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        shopInfoEntity.set(response.body().getData());
                        activity.setData();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ShopInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void saveShopInfo() {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().saveShopInfo(shopInfoEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        MToast.showToast(activity, "保存成功");
                        activity.finish();
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }

    public void getCategoryList(String areaCode) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getShoppingCarAPI().getCategoryList(areaCode).enqueue(new Callback<BaseEntity<List<CategoryParentsEntity<List<CategoryChildEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CategoryParentsEntity<List<CategoryChildEntity>>>>> call, Response<BaseEntity<List<CategoryParentsEntity<List<CategoryChildEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if ("ok".equals(response.body().getState())) {
                    if (response.body().getData() != null) {
                        MToast.showToast(activity, "保存成功");
                    }
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CategoryParentsEntity<List<CategoryChildEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    public String getProvinceCodeModel() {
        return repertory.getAreaCode();
    }
}
